home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / castools.zip / GETENTRY.C < prev    next >
Text File  |  1990-02-02  |  4KB  |  153 lines

  1.  
  2.  
  3. /*
  4.    GETENTRY.C  Function PbLookUpEntry: Gets a phonebook entry (individual
  5.       or group) based on its name (exact) or record ID.
  6.  
  7.    INPUT:  Phonebook structure, possibly memory space to hold the entry, and
  8.       an exact name or record ID.
  9.  
  10.    OUTPUT: If successful, a pointer to a phonebook entry structure holding the
  11.       requested entry.
  12. */
  13.  
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <malloc.h>
  17. #include <string.h>
  18. #include <phonebk.h>
  19.  
  20. PBE * pascal PbGetEntry(PB *pb, PBE *entry, char *name, int RecordID)
  21. {
  22.   PBE *pbe;
  23.   PBEFIXED *result;              /* return for GetFixedPart */
  24.   int red;                       /* return value for fread() */
  25.   PBEFIXED fixed_part;           /* for the known-length part of entry */
  26.   char *vbl_part = NULL;         /* for the variable-length part of entry */
  27.   size_t vbl_length;             /* length of variable-length part of entry */
  28.   int i, j, k;                   /* loop counters */
  29.   int temperrno = 0;             /* for saving aside Pberrno */
  30.  
  31.   Pberrno = 0;                    /* Initially, always reset */
  32.  
  33.   /* First, check params */
  34.   if ((pb == NULL) ||
  35.       ((name == NULL) && (RecordID == -1))) {
  36.     Pberrno = INVALIDPARAMETER;
  37.     return(NULL);
  38.   }
  39.   if ((RecordID < -1) ||
  40.       (RecordID > 999)) {
  41.     Pberrno = INVALIDPARAMETER;
  42.     return(NULL);
  43.   }
  44.  
  45.   if (RecordID == -1) {
  46.     for (RecordID = 0; RecordID < MAXENTRIES; RecordID++) {
  47.       result = GetFixedPart(pb, &fixed_part, RecordID);
  48.       if (result) {
  49.         if (!(strnicmp(fixed_part.name, name, NAMELENGTH))) {
  50.           break;
  51.         }
  52.       }
  53.       else {
  54.         if (Pberrno) {              /* GetFixedPart encountered an error */
  55.           return(NULL);
  56.         }
  57.       }
  58.     }
  59.     if (RecordID == MAXENTRIES) {
  60.       Pberrno = NOENTRYFOUND;
  61.       return(NULL);
  62.     }
  63.   }
  64.  
  65.   /* ELSE:  The record id is given: ignore name, if any */
  66.   else {
  67.     result = GetFixedPart(pb, &fixed_part, RecordID);
  68.     if (!result) {
  69.       Pberrno = NOENTRYPRESENT;
  70.       return(NULL);
  71.     }
  72.   }
  73.  
  74.   /* Now the fixed part is gotten: prepare final destination, and fill it in */
  75.   if (!entry) {
  76.     pbe = (PBE *)calloc(1, sizeof(PBE));
  77.     if (!pbe) {
  78.       Pberrno = OUTOFMEM;
  79.       return(NULL);
  80.     }
  81.     if (fixed_part.type == PERSONENTRY) {
  82.       if (!(pbe->fields = (char **)calloc(pb->header.fields,
  83.                                           sizeof(char *)))) {
  84.         Pberrno = OUTOFMEM;
  85.         goto error_out;
  86.       }
  87.       for (i=0; i<pb->header.fields; i++) {
  88.         if (!(pbe->fields[i] = (char *)calloc(60, sizeof(char)))) {
  89.           Pberrno = OUTOFMEM;
  90.           goto error_out;
  91.         }
  92.       }
  93.     }
  94.     if (!(pbe->MemberList = (int *)malloc(fixed_part.members * sizeof(int)))) {
  95.       Pberrno = OUTOFMEM;
  96.       goto error_out;
  97.     }
  98.   }
  99.   else {
  100.     pbe = entry;
  101.   }
  102.   memcpy(pbe, &fixed_part, sizeof(PBEFIXED));
  103.  
  104.   /* Read the rest of the entry into the complete entry structure */
  105.   if (vbl_length = pbe->length - sizeof(PBEFIXED)) {
  106.     if (!(vbl_part = (char *)malloc(vbl_length))) {
  107.       Pberrno = OUTOFMEM;
  108.       goto error_out;
  109.     }
  110.     red = fread(vbl_part, 1, vbl_length, pb->fp);
  111.     if (red != vbl_length) {
  112.       Pberrno = CANTREAD;
  113.       goto error_out;
  114.     }
  115.  
  116.     /* if read successful, fill in entry's variable fields */
  117.     if (pbe->type == PERSONENTRY) {
  118.       for (i=0, j=0; i<pb->header.fields; i++, j++) {
  119.         for (k=0; k<MAXFIELDSIZE; k++, j++) {
  120.           if (!(pbe->fields[i][k] = vbl_part[j])) {
  121.             break;
  122.           }
  123.         }
  124.       }
  125.     }
  126.     else {
  127.       j = 0;
  128.     }
  129.     for (i=0; i<pbe->members; i++) {
  130.       pbe->MemberList[i] = ((int *)(vbl_part + j))[i];
  131.     }
  132.     free(vbl_part);
  133.   }
  134.  
  135.   /* If we got here, all went well! */
  136.   return(pbe);
  137.  
  138. error_out:
  139.   if (vbl_part) {
  140.     free(vbl_part);
  141.   }
  142.   if (Pberrno) {
  143.     temperrno = Pberrno;
  144.   }
  145.   if (!entry) {
  146.     PbFreePBE(pb, pbe);
  147.   }
  148.   if (temperrno) {
  149.     Pberrno = temperrno;
  150.   }
  151.   return(NULL);
  152. }
  153.